The Object.clone
/ java.lang.Cloneable
mechanism in Java should be considered broken for the following reasons and
should, consequently, not be used:
-
Cloneable
is a marker interface without API but with a contract about class behavior that the compiler cannot enforce.
This is a bad practice.
- Classes are instantiated without calling their constructor, so possible preconditions cannot be enforced.
- There are implementation flaws by design when overriding
Object.clone
, like type casts or the handling of
CloneNotSupportedException
exceptions.